//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartCombinedView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartCombinedView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartCombinedView

IMPLEMENT_DYNCREATE(CChartCombinedView, CBCGPChartExampleView)

CChartCombinedView::CChartCombinedView()
	: CBCGPChartExampleView(CChartCombinedView::IDD)
{
	//{{AFX_DATA_INIT(CChartCombinedView)
	m_bShowDataMarkers = TRUE;
	m_nMarkerSize = 0;
	m_nMarkerShape = 0;
	m_bShowDataLabels = TRUE;
	m_nDataLabelAngle = 2;
	//}}AFX_DATA_INIT
}

CChartCombinedView::~CChartCombinedView()
{
}

void CChartCombinedView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartCombinedView)
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Control(pDX, IDC_DATA_LABEL_ANGLE, m_wndDataLabelAngle);
	DDX_Control(pDX, IDC_DATA_MARKER_SHAPE, m_wndMarkerShape);
	DDX_Control(pDX, IDC_DATA_MARKER_SIZE, m_wndMarkerSize);
	DDX_Check(pDX, IDC_DATA_MARKERS, m_bShowDataMarkers);
	DDX_CBIndex(pDX, IDC_DATA_MARKER_SIZE, m_nMarkerSize);
	DDX_CBIndex(pDX, IDC_DATA_MARKER_SHAPE, m_nMarkerShape);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_ANGLE, m_nDataLabelAngle);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartCombinedView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartCombinedView)
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_MARKER_SHAPE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_MARKER_SIZE, OnUpdateChart)
	ON_BN_CLICKED(IDC_DATA_MARKERS, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_LABEL_ANGLE, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartCombinedView diagnostics

#ifdef _DEBUG
void CChartCombinedView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartCombinedView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartCombinedView message handlers

void CChartCombinedView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartTitle(_T("Area, Column and Line:"));

	CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("Area"), CBCGPColor(), BCGP_CT_DEFAULT, BCGPChartArea);
	CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("Column"), CBCGPColor(), BCGP_CT_DEFAULT, BCGPChartColumn);
	CBCGPChartSeries* pSeries3 = pChart->CreateSeries(_T("Line"), CBCGPColor(), BCGP_CT_DEFAULT, BCGPChartLine);

	pChart->SetThemeOpacity(70);

	COleDateTime today = COleDateTime::GetCurrentTime();

	double arTemp[3][12] = {
		{ 5., 6., 7, 14, 19, 21, 26, 27, 28, 31, 33, 38 },
		{ 5., 12., 19, 20, 15, 12, 14, 23, 18, 14, 10, 7 },
		{ 35., 32., 29, 26, 25, 24, 21, 19, 12, 8, 6, 0 }};

	for (int nMonth = 1; nMonth <= 12; nMonth++)
	{
		COleDateTime date (today.GetYear(), nMonth, 1, 0, 0, 0);

		pSeries1->AddDataPoint(date.Format(_T("%b")), arTemp[0][nMonth - 1]);

		pSeries2->AddDataPoint(arTemp[1][nMonth - 1]);

		pSeries3->AddDataPoint(arTemp[2][nMonth - 1]);
	}

	BCGPChartFormatSeries style = pSeries3->GetSeriesFormat();

	style.m_curveType = BCGPChartFormatSeries::CCT_SPLINE_HERMITE;
	style.SetSeriesLineWidth(3);

	pSeries3->SetSeriesFormat(style);

	OnUpdateChart();
}

void CChartCombinedView::OnUpdateChart()
{
	UpdateData();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	m_wndMarkerShape.EnableWindow(m_bShowDataMarkers);
	m_wndMarkerSize.EnableWindow(m_bShowDataMarkers);

	m_wndDataLabelAngle.EnableWindow(m_bShowDataLabels);

	pChart->ShowDataMarkers(m_bShowDataMarkers, max(7, 5 * (m_nMarkerSize + 1)), 
		(BCGPChartMarkerOptions::MarkerShape)m_nMarkerShape);

	double dblDataLabelAngle = (m_nDataLabelAngle - 2) * 45;

	pChart->ShowDataLabels(m_bShowDataLabels, TRUE, TRUE, dblDataLabelAngle);

	pChart->Redraw();
}
